/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yoga.algo.phenotype;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mushu
 */
public class PopulationStatisticsDataTable
{
    private final ArrayList<StatisticsDataRow> m_listDataRows = new ArrayList<StatisticsDataRow>();
    private final String Delimiter = "\t";
    private final String NewLine = "\n";

    public class StatisticsDataRow
    {
        public double MeanFitnessValue;
        public double MedianFitnessValue;
        public double MinFitnessValue;
        public double MaxFitnessValue;
        public int Counter;
    }

    public void addStatistics(PopulationStatistics populationStatistics)
    {
        StatisticsDataRow dataRow = new StatisticsDataRow();
        dataRow.MeanFitnessValue = populationStatistics.getMeanFitnessValue();
        dataRow.MaxFitnessValue = populationStatistics.getMaxFitnessValue();
        dataRow.MedianFitnessValue = populationStatistics.getMedianFitnessValue();
        dataRow.MinFitnessValue = populationStatistics.getMinFitnessValue();

        m_listDataRows.add(dataRow);
    }

    public void clear()
    {
        m_listDataRows.clear();
    }

    public void saveToFile(File file)
    {
        FileWriter fileWriter;
        try
        {
            fileWriter = new FileWriter(file);
            fileWriter.write("Number" + Delimiter + "Min_Fitness" + Delimiter + "Mean_Fitness" + Delimiter + "Median_Fitness" + Delimiter + "Max_Fitness" + NewLine);
            int counter = 0;
            for (StatisticsDataRow dataRow : m_listDataRows)
            {
                counter++;
                fileWriter.write("" + counter);
                fileWriter.write(Delimiter);
                fileWriter.write(""+dataRow.MinFitnessValue);
                fileWriter.write(Delimiter);
                fileWriter.write("" + dataRow.MeanFitnessValue);
                fileWriter.write(Delimiter);
                fileWriter.write("" + dataRow.MedianFitnessValue);
                fileWriter.write(Delimiter);
                fileWriter.write("" + dataRow.MaxFitnessValue);
                fileWriter.write(NewLine);        
            }
            
            fileWriter.close();
        }
        catch (IOException ex)
        {
            Logger.getLogger(PopulationStatisticsDataTable.class.getName()).log(Level.SEVERE, null, ex);
        }



    }
}
